﻿/// <reference path="../ExtJs/ext-all.js" />
Ext.BLANK_IMAGE_URL = 'ext/resources/images/default/s.gif';
var webPath = "";
Ext.namespace("Suucha");
var currtWind;

function GetUrlParms() {
    var args = new Object();
    var query = location.search.substring(1); //获取查询串 
    var pairs = query.split("&"); //在逗号处断开 
    for (var i = 0; i < pairs.length; i++) {
        var pos = pairs[i].indexOf('='); //查找name=value 
        if (pos == -1)
            continue; //如果没有找到就跳过 
        var argname = pairs[i].substring(0, pos); //提取name 
        var value = pairs[i].substring(pos + 1); //提取value 
        args[argname] = decodeURI(value); //存为属性 
    }
    return args;
}

function showErrorMessage(title, msg, details) {

    Ext.MessageBox.show({
        title: title,
        msg: msg,
        buttons: Ext.Msg.OK,
        icon: Ext.MessageBox.ERROR,
        miniWidth: 200
    });
}

function showMessage(title, msg) {
    Ext.MessageBox.show({
        title: title,
        msg: msg,
        icon: Ext.MessageBox.INFO,
        buttons: Ext.Msg.OK
    });
}

var Cookies = {};
Cookies.set = function(name, value) {
    var argv = arguments;
    var argc = arguments.length;
    var expires = (argc > 2) ? argv[2] : null;
    var path = (argc > 3) ? argv[3] : '/';
    var domain = (argc > 4) ? argv[4] : null;
    var secure = (argc > 5) ? argv[5] : false;
    document.cookie = name + "=" + escape(value) +
    ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
    ((path == null) ? "" : ("; path=" + path)) +
    ((domain == null) ? "" : ("; domain=" + domain)) +
    ((secure == true) ? "; secure" : "");
};

Cookies.get = function(name) {
    var arg = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;
    var j = 0;
    while (i < clen) {
        j = i + alen;
        if (document.cookie.substring(i, j) == arg)
            return Cookies.getCookieVal(j);
        i = document.cookie.indexOf(" ", i) + 1;
        if (i == 0)
            break;
    }
    return null;
};

Cookies.clear = function(name) {
    if (Cookies.get(name)) {
        document.cookie = name + "=" +
        "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
};

Cookies.getCookieVal = function(offset) {
    var endstr = document.cookie.indexOf(";", offset);
    if (endstr == -1) {
        endstr = document.cookie.length;
    }
    return unescape(document.cookie.substring(offset, endstr));
};
function openWind(url, id, text, width, height, iconCls) {
        win = new Ext.Window({
            layout: 'fit',
            width: width,
            height: height,
            id: id,
            border: false,
            iconCls: iconCls,
            shadow: true,
            animate: true,
            collapsible: true,
            autoDestroy: true,
            closable: true,
            closeAction: 'close',
            modal: true,
            title: text,
            autoLoad: { url: url, scripts: true }
        });
        win.show();
        return win;
}
function openWindExt(id, text, width, height, iconCls, items, buttons) {
    win = new Ext.Window({
        layout: 'fit',
        width: width,
        height: height,
        id: id,
        border: false,
        iconCls: iconCls,
        shadow: true,
        animate: true,
        collapsible: true,
        autoDestroy: true,
        closable: true,
        closeAction: 'close',
        modal: true,
        title: text,
        items: items,
        buttons: buttons
    });
    win.show();
    return win;
}

function openWindHtml(html, id, text, width, height, iconCls) {
    win = new Ext.Window({
        layout: 'fit',
        width: width,
        height: height,
        id: id,
        border: false,
        iconCls: iconCls,
        shadow: true,
        animate: true,
        collapsible: true,
        autoDestroy: true,
        closable: true,
        closeAction: 'close',
        modal: true,
        title: text,
        html:html
    });
    win.show();
    return win;
}

function ajaxCalled(sender, url,dataStr, grid, fn) {
    if (!fn) {
        fn = function validateFeed(response, options) {
            var responseArray = Ext.util.JSON.decode(response.responseText);
            if (eval(responseArray.success)) {
                Ext.MessageBox.show({
                    title: '操作提示',
                    msg: responseArray.message,
                    buttons: Ext.Msg.OK,
                    icon: Ext.MessageBox.INFO,
                    miniWidth: 200
                });
                if (grid)
                    updata(grid);
                if (sender)
                    sender.close();
            }
            else {
                Ext.MessageBox.show({
                    title: '错误提示',
                    msg: responseArray.message,
                    buttons: Ext.Msg.OK,
                    icon: Ext.MessageBox.ERROR,
                    miniWidth: 200
                });
            }
        };
    }
    Ext.Ajax.request({
    url: url,
    method: 'post',
    url: url,
    params: { data: dataStr },
        success: fn
    });
}

function TwoajaxCalled(sender, url, dataStr1,dataStr2,grid, fn) {
    if (!fn) {
        fn = function validateFeed(response, options) {
            var responseArray = Ext.util.JSON.decode(response.responseText);
            if (eval(responseArray.success)) {
                Ext.MessageBox.show({
                    title: '操作提示',
                    msg: responseArray.message,
                    buttons: Ext.Msg.OK,
                    icon: Ext.MessageBox.INFO,
                    miniWidth: 200
                });
                if (grid)
                    updata(grid);
                if (sender)
                    sender.close();
            }
            else {
                Ext.MessageBox.show({
                    title: '错误提示',
                    msg: responseArray.message,
                    buttons: Ext.Msg.OK,
                    icon: Ext.MessageBox.ERROR,
                    miniWidth: 200
                });
            }
        };
    }
    Ext.Ajax.request({
        url: url,
        method: 'post',
        url: url,
        params: { data: dataStr1, data1: dataStr2 },
          success: fn
    });
}

function updata(grid) {
    grid.getStore().removeAll();
    grid.getStore().reload();
}
function checkGridSelected(grid) {
//Ext3.0以上版本 更改获取grid行的值
    var selModel = grid.getSelectionModel().getSelections();
    if (selModel != null && selModel != '') {
        return selModel;
    }
    else {
        Ext.MessageBox.show({
            title: '操作提示',
            msg: "请选中要操作的记录",
            buttons: Ext.Msg.OK,
            icon: Ext.MessageBox.INFO,
            miniWidth: 200,
            animEl: 'deletebtn'
        });
        return false;
    }
}

function getIdFromGrid(nodes, item) {
    var id = '';
    for (var i = 0; i < nodes.length; i++) {
        id += nodes[i].get(item) + ",";
    }
    id = id.substring(0, id.length - 1);
    return id;
}
var obj = new Object();
obj = GetUrlParms();
var ArchivesType = obj['ArchivesTypeKey'];
if (!ArchivesType)
    ArchivesType = 1;
function GUID() {
    return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
}
function S4() {
    return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
}

/*************************************
争对form表单提交，然后刷新相关的grid
*************************************/
function submitFormJson(form, grid, sender, fn, data, urlExt, witMsg, showSuccessMsg) {
    if (form.form.isValid()) {

        var myMask = new Ext.LoadMask(form.getEl(), {
            msg: witMsg || "正在保存数据,请稍候...."
        });
        var dataStr = Ext.util.JSON.encode(form.form.getValues());
        if (data) {
            dataStr = dataStr.substring(0, dataStr.length - 1) + "," + data + "}";
        }

        myMask.show();
        var url = form.url;
        if (urlExt) {
            url += urlExt;
        }
        Ext.Ajax.request({
            method: 'post',
            url: url,
            params: { data: dataStr },
            success: onSuccess,
            failure: onFailure
        });
        function onSuccess(request, options) {
            // 服务器返回json形式的结果
            myMask.hide();
            var reText = request.responseText;
            var reTextObj = Ext.util.JSON.decode(reText);
            if (eval(reTextObj.success)) {
                if (!showSuccessMsg)
                    showMessage('操作成功', reTextObj.message);
                if (grid)
                    updata(grid);
//                if (sender)
//                    sender.close();
                if (fn)
                    fn.call(this, reTextObj.message);
            } else {
                showErrorMessage('操作失败', reTextObj.message, '');
            }
        }
        function onFailure(request, options) {
            myMask.hide();
            showErrorMessage('操作失败', request.responseText, '');
        }
    }
}

/*************************************
form表单Image提交，然后刷新相关的grid
*************************************/
function submitFormJsonImage(form, grid, sender, fn) {
    if (form.form.isValid()) {
        var myMask = new Ext.LoadMask(form.getEl(), {
            msg: "正在保存数据,请稍候...."
        });


        var dataStr = Ext.util.JSON.encode(form.form.getValues());
 
        myMask.show();
        //        alert(dataStr);

        Ext.Ajax.request({
            method: 'post',
            url: form.url,
            params: { data: dataStr },
            success: onSuccess,
            failure: onFailure
        });
        function onSuccess(request, options) {
            // 服务器返回json形式的结果
            myMask.hide();
            var reText = request.responseText;
            var reTextObj = Ext.util.JSON.decode(reText);
            if (eval(reTextObj.success)) {
                showMessage('操作成功', reTextObj.message);
                if (grid)
                    updata(grid);
                if (sender)
                    sender.close();
                if (fn)
                    fn.call(this, reText);
            } else {
                showErrorMessage('操作失败', reTextObj.message, '');
            }
        }
        function onFailure(request, options) {
            myMask.hide();
            showErrorMessage('操作失败', request.responseText, '');
        }
    }
}

function CheckIsCanEdit(nodes, mark) {
    var rb = true;
    for (var i = 0; i < nodes.length; i++) {
        if (nodes[i].get('F40').toString() != policemanNumber) {
            rb = false;
            break;
        }
    }
    return rb;
}
var boolData = [
    ['是', '1'],
    ['否', '0']
    ]
var boolStore = new Ext.data.SimpleStore({
    fields: ['name', 'value']
});
boolStore.loadData(boolData);
var sexData = [
    ['男', '1'],
    ['女', '2'],
    ['未知性别', '3'],
    ['未说明性别', '4']
    ]
var sexStore = new Ext.data.SimpleStore({
    fields: ['name', 'value']
});
sexStore.loadData(sexData);

